﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

namespace Hisun.View.ReportQuery
{
    public partial class Report_SWPCXXB : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        string CenterIdArray = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist_Year(DropDownList_Year, Application["accountPeriod"].ToString());


                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                DropDownList_Product.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                DropDownList_Product.Items.Clear();
                if (CenterIdArray == "")
                {

                }
                else
                {
                    string[] CenterIDs = CenterIdArray.Split(',');
                    HiddenField_CenterID.Value = CenterIdArray;

                    var myProductList = (from a in db.TABC_JS_ProductInProcess
                                         join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                         where CenterIDs.Contains(a.作业中心ID.ToString())
                                         && a.可用 == true
                                         orderby a.产品ID
                                         select new
                                         {
                                             a.产品ID,
                                             b.产品账户名称
                                         }).Distinct();


                    foreach (var myProduct in myProductList)
                    {
                        DropDownList_Product.Items.Add(new ListItem(myProduct.产品ID + "|" + myProduct.产品账户名称, myProduct.产品ID));

                    }

                }
            }
        }



        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenters = new ChildCenterList(CenterID);

            var myCenterFilters = myCenters.CostCenterlist.Where(p => p.停用 == false);


            foreach (var myCenterFilter in myCenterFilters)
            {
                CenterIdArray = CenterIdArray + "," + myCenterFilter.作业中心ID.ToString();

            }


        }



        protected void Button_QueryLine_Click(object sender, EventArgs e)
        {



            CostReport MyReport = new CostReport();
            string myProductAccount = DropDownList_Product.SelectedValue.ToString();

            string myYear = DropDownList_Year.SelectedValue.ToString();
            string UserName = Session[ServerVars.sysSessionUserName].ToString();
            string myHtmlReport = "";

            myHtmlReport = MyReport.WriteExcel_Report_SWPCXXB(myProductAccount, myYear, UserName);
            Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");
         
        }
    }
}
